﻿using System;
using System.Data;
using System.Data.SQLite;
using System.IO;

namespace YArchitech.MEP.Electric.Common
{
	public sealed class SQLiteDatabaseAssist
	{
		public SQLiteDatabaseAssist(string dbFile)
		{
			this.m_DataSource = dbFile;
		}

		public bool Open()
		{
			if (this.m_Connection != null && this.m_Connection.State != ConnectionState.Closed)
			{
				this.m_Connection.Close();
				this.m_Connection = null;
			}
			if (string.IsNullOrWhiteSpace(this.m_DataSource) || !File.Exists(this.m_DataSource))
			{
				return false;
			}
			string connectionString = string.Format("Data Source={0};Pooling=true;FailIfMissing=false", this.m_DataSource);
			this.m_Connection = new SQLiteConnection(connectionString);
			this.m_Connection.Open();
			return true;
		}

		public bool IsOpened()
		{
			return this.m_Connection != null && this.m_Connection.State != ConnectionState.Closed;
		}

		public void Close()
		{
			if (this.IsOpened())
			{
				this.m_Connection.Close();
				this.m_Connection = null;
				return;
			}
			this.m_Connection = null;
		}

		public void ResetDataSource(string dbFile)
		{
			if (dbFile == this.m_DataSource)
			{
				return;
			}
			this.Close();
			this.m_DataSource = dbFile;
		}

		public string DataSource
		{
			get
			{
				return this.m_DataSource;
			}
		}

		public SQLiteConnection Connection
		{
			get
			{
				return this.m_Connection;
			}
		}

		public void BeginTransaction()
		{
			if (!this.IsOpened() && !this.Open())
			{
				return;
			}
			this.m_Trans = this.m_Connection.BeginTransaction();
		}

		public void CommitTransaction()
		{
			if (this.m_Trans == null)
			{
				return;
			}
			this.m_Trans.Commit();
		}

		public void RollbackTransaction()
		{
			if (this.m_Trans == null)
			{
				return;
			}
			this.m_Trans.Rollback();
		}

		public DataTable ExecuteQuery(string sqlStr)
		{
			if (!this.IsOpened() && !this.Open())
			{
				return null;
			}
			DataTable dataTable = new DataTable();
			DataTable result;
			using (SQLiteCommand sqliteCommand = this.m_Connection.CreateCommand())
			{
				try
				{
					sqliteCommand.CommandText = sqlStr;
					sqliteCommand.CommandType = CommandType.Text;
					IDataReader reader = sqliteCommand.ExecuteReader();
					dataTable.Load(reader);
					result = dataTable;
				}
				catch (Exception ex)
				{
					throw ex;
				}
			}
			return result;
		}

		public bool ExecuteCommand(string sqlStr)
		{
			if (!this.IsOpened() && !this.Open())
			{
				return false;
			}
			bool result;
			using (SQLiteCommand sqliteCommand = this.m_Connection.CreateCommand())
			{
				try
				{
					sqliteCommand.CommandText = sqlStr;
					sqliteCommand.CommandType = CommandType.Text;
					result = (sqliteCommand.ExecuteNonQuery() > 0);
				}
				catch (Exception ex)
				{
					throw ex;
				}
			}
			return result;
		}

		public bool ExecuteCommandEx(string sqlStr)
		{
			if (!this.IsOpened() && !this.Open())
			{
				return false;
			}
			bool result;
			using (SQLiteCommand sqliteCommand = this.m_Connection.CreateCommand())
			{
				try
				{
					sqliteCommand.CommandText = sqlStr;
					sqliteCommand.CommandType = CommandType.Text;
					sqliteCommand.Transaction = this.m_Trans;
					result = (sqliteCommand.ExecuteNonQuery() > 0);
				}
				catch (Exception ex)
				{
					throw ex;
				}
			}
			return result;
		}

		public bool ExecuteScalar(string sqlStr, out int uId)
		{
			uId = -1;
			if (!this.IsOpened() && !this.Open())
			{
				return false;
			}
			bool result;
			using (SQLiteCommand sqliteCommand = this.m_Connection.CreateCommand())
			{
				try
				{
					sqliteCommand.CommandText = sqlStr;
					sqliteCommand.CommandType = CommandType.Text;
					uId = Convert.ToInt32(sqliteCommand.ExecuteScalar());
					result = (uId != -1);
				}
				catch (Exception ex)
				{
					throw ex;
				}
			}
			return result;
		}

		public bool ExecuteScalarEx(string sqlStr, out int uId)
		{
			uId = -1;
			if (!this.IsOpened() && !this.Open())
			{
				return false;
			}
			bool result;
			using (SQLiteCommand sqliteCommand = this.m_Connection.CreateCommand())
			{
				try
				{
					sqliteCommand.CommandText = sqlStr;
					sqliteCommand.CommandType = CommandType.Text;
					sqliteCommand.Transaction = this.m_Trans;
					uId = Convert.ToInt32(sqliteCommand.ExecuteScalar());
					result = (uId != -1);
				}
				catch (Exception ex)
				{
					throw ex;
				}
			}
			return result;
		}

		private string m_DataSource = string.Empty;

		private SQLiteConnection m_Connection;

		private SQLiteTransaction m_Trans;
	}
}
